package com.penghaisoft.microwms.material.web.expose.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.RawRequisition;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.RawMesKittingDetailDto;
import com.penghaisoft.microwms.common.dto.RawMesKittingDto;
import com.penghaisoft.microwms.common.dto.RawStorageDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IMaterialInsideService;
import com.penghaisoft.microwms.material.web.outwarehouse.service.IRawRequisitionService;

/**
 * @Description 原材料领料单
 * @author luoteng
 * @date 2018年6月19日 上午11:00:27 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/expose/mes")
public class ExposeMesController extends BaseController {


    @Autowired
    private IRawRequisitionService rawRequisitionService;
    
    @Autowired
    private IMaterialInsideService materialInsideService;
    
    /** 
     * @Title requisitionNormalAdd
     * @Description 原材料领料单 新增
     * @author luoteng
     * @date 2018年6月19日:下午1:49:19
     * @param list
     * @return HandResp
     */
    @PostMapping("rawrequisition/normal/add")
    public HandResp requisitionNormalAdd(@RequestBody List<RawRequisition> list) {
        HandResp result = new HandResp();
        try{
        	Resp resp = rawRequisitionService.batchNormalInsert(list);
            result.setCode(resp.getCode());
            result.setMsg(resp.getMsg());
        }catch(RuntimeException e){
        	result.setCode(Constant.FAIL);
            result.setMsg(e.getMessage());
            rawRequisitionService.batchNormalHisInsert(list, e.getMessage());
        }
        
        return result;
    }
    
    /** 
     * @Title requisitionRepairAdd
     * @Description 原材料领料单 返修 新增
     * @author luoteng
     * @date 2018年6月19日:下午1:49:27
     * @param list
     * @return HandResp
     */
    @PostMapping("rawrequisition/repair/add")
    public HandResp requisitionRepairAdd(@RequestBody List<RawRequisition> list) {
        HandResp result = new HandResp();

        Resp resp = rawRequisitionService.batchRepairInsert(list);
        result.setCode(resp.getCode());
        result.setMsg(resp.getMsg());
        
        return result;
    }
    
    /** 
     * @Title queryKitting
     * @Description 获取原材料齐套率
     * @author luoteng
     * @date 2018年7月16日:下午1:36:10
     * @param rawMesKittingDto
     * @return HandResp
     */
    @PostMapping("kitting/query")
    public HandResp queryKitting(@RequestBody RawMesKittingDto rawMesKittingDto) {
        HandResp result = new HandResp();
        
        List<RawMesKittingDetailDto> details = rawMesKittingDto.getDetails();
        if(details == null || details.isEmpty() || rawMesKittingDto.getSalesCode() == null || rawMesKittingDto.getSalesCode().equals("")){
        	result.setCode(Constant.FAIL);
            result.setMsg("参数传递错误！");
            return result;
        }
        
        StringBuffer sb = new StringBuffer();
        for(RawMesKittingDetailDto dto : details){
        	if(dto.getPlanNum() == null){
        		result.setCode(Constant.FAIL);
                result.setMsg("参数传递错误（需求数量为空）！");
                return result;
    		}
        	RawStorageDto rawStorageDto = new RawStorageDto();
        	rawStorageDto.setContractNo(rawMesKittingDto.getSalesCode());
        	rawStorageDto.setModel(dto.getMaterialCode());
        	rawStorageDto.setModelType(dto.getMaterialType());
        	List<RawStorageDto> list = materialInsideService.queryAvailAmountList(rawStorageDto);
        	if(list == null || list.isEmpty()){
        		sb.append(dto.getMaterialCode()+"没有库存；");
        	}else{
        		RawStorageDto tmp = list.get(0);
        		BigDecimal storageAmount;
        		if(dto.getMaterialType().equals(Constant.MODEL_WT) || dto.getMaterialType().equals(Constant.MODEL_ST) 
        				|| dto.getMaterialType().equals(Constant.MODEL_BJ) || dto.getMaterialType().equals(Constant.MODEL_GD)){
        			storageAmount = tmp.getAmount() == null ? BigDecimal.ZERO : tmp.getAmount();
        		}else{
        			storageAmount = tmp.getAvailAmount() == null ? BigDecimal.ZERO : tmp.getAvailAmount();
        		}
//        		PE管缺少10米之内，可以拆分生产订单
        		Boolean peFlag = false;
        		for(String str : Constant.PE_LIST) {
    				if(str.equals(dto.getMaterialType())) {
    					peFlag = true;
    					break;
    				}
    			}
        		if(peFlag){
        			if(dto.getPlanNum().compareTo(storageAmount.add(Constant.PE_DIFF)) > 0){
            			sb.append(dto.getMaterialCode()+"缺少" + dto.getPlanNum().subtract(storageAmount) + dto.getMeasureUnit() + "；");
            		}
        		}else{
        			if(dto.getPlanNum().compareTo(storageAmount) > 0){
            			sb.append(dto.getMaterialCode()+"缺少" + dto.getPlanNum().subtract(storageAmount) + dto.getMeasureUnit() + "；");
            		}
        		}
        	}
        }
        
        if(sb.length() > 0){
//        	0 不齐套 1齐套
        	result.setCode(Constant.FAIL);
            result.setMsg(sb.toString());
        }else{
        	result.setCode(Constant.SUCCESS);
            result.setMsg("已经齐套！");
        }
        return result;
    }
}
